package com.snail.utils;


import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.ai.openai.OpenAiAudioTranscriptionOptions;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * 连接模型进行语音识别
 */
@Service
public class TranscriptionServiceUtil {

    @Autowired
    private OpenAiAudioTranscriptionModel transcriptionModel;

    public String transcribeAudioFile(File audioFile) {
        var transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
                .model("FunAudioLLM/SenseVoiceSmall")
                .responseFormat(OpenAiAudioApi.TranscriptResponseFormat.SRT)
                .build();

        Resource audioResource = new FileSystemResource(audioFile);
        AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(audioResource, transcriptionOptions);
        AudioTranscriptionResponse response = transcriptionModel.call(prompt);

        return response.getResult().getOutput();
    }
}